package cc.rengu.igas.channel.nucc.core.service.trans;

import cc.rengu.igas.channel.nucc.common.constant.NuccParamConstant;
import cc.rengu.igas.channel.nucc.common.constant.NuccTreeNodeConstant;
import cc.rengu.igas.channel.nucc.common.enums.TrxCtgyCdEnum;
import cc.rengu.igas.channel.nucc.core.realize.NuccSeqNoManage;
import cc.rengu.igas.channel.nucc.core.realize.NuccService;
import cc.rengu.igas.channel.nucc.core.realize.impl.NuccSeqNoManageImpl;
import cc.rengu.igas.channel.nucc.core.realize.impl.NuccServiceImpl;
import cc.rengu.igas.channel.nucc.core.service.base.NuccBaseService;
import cc.rengu.igas.channel.nucc.facade.bean.*;
import cc.rengu.igas.channel.nucc.facade.request.VerifySignAcqRequest;
import cc.rengu.igas.channel.nucc.facade.request.VerifySignRequest;
import cc.rengu.igas.channel.nucc.facade.response.ChnlBizResponse;
import cc.rengu.igas.channel.nucc.facade.response.VerifySignAcqResponse;
import cc.rengu.igas.channel.nucc.facade.response.VerifySignGwResponse;
import cc.rengu.igas.channel.nucc.facade.response.VerifySignResponse;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.utility.util.*;


/**
 * 网联身份认证及申请服务
 */
public class EpccVerifySignService extends NuccBaseService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        VerifySignRequest verifySignRequest = new VerifySignRequest();
        ConvertUtil.convertOutput(verifySignRequest);
        return verifySignRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        VerifySignRequest innerRequest = (VerifySignRequest) request;
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ISSR_ID, innerRequest.getInstgId());

        VerifySignResponse response = new VerifySignResponse();
        NuccService nuccService = new NuccServiceImpl();
        VerifySignAcqRequest acqRequest = initVerifySignAcqRequest(xmlTreeUtil, innerRequest);

        ChnlBizResponse bizResponse = new ChnlBizResponse();

        /*网关签约*/
        if (TrxCtgyCdEnum.SIGN_GW.getType().equals(innerRequest.getTrxCtgy())) {
            VerifySignGwResponse acqResponse = nuccService.verifySignGw(acqRequest);
            if (!NuccParamConstant.EPCC_SUCCESS_CODE.equals(acqResponse.getSysRtnCd())) {
                rglog.error("调用网联接口失败，sysRtnCd=[{}],sysRtnDesc=[{}]", acqResponse.getSysRtnCd(), acqResponse.getSysRtnDesc());

                response.setRespCode(acqResponse.getSysRtnCd());
                response.setRespDesc(acqResponse.getSysRtnDesc());

            } else {
                response.setDgtlEnvlp(acqResponse.getDgtlEnvlp());
                response.setNcrptnSn(acqResponse.getNcrptnSn());
                response.setRdrctUrl(acqResponse.getRdrctUrl());
                response.setSec(acqResponse.getSec());
                response.setRespCode(acqResponse.getSysRtnCd());
                response.setRespDesc(acqResponse.getSysRtnDesc());
            }
        } else {
            /*快捷签约*/
            VerifySignAcqResponse acqResponse = nuccService.verifySignQuick(acqRequest);
            if (!NuccParamConstant.EPCC_SUCCESS_CODE.equals(acqResponse.getSysRtnCd())) {
                rglog.error("调用网联接口失败，sysRtnCd=[{}],sysRtnDesc=[{}]", acqResponse.getSysRtnCd(), acqResponse.getSysRtnDesc());
                response.setRespCode(acqResponse.getSysRtnCd());
                response.setRespDesc(acqResponse.getSysRtnDesc());
            } else {
                if (TrxCtgyCdEnum.VERIFY.getType().equals(innerRequest.getTrxCtgy())) {
                    /*快捷身份交易，返回短信验证码流水（通道流水号）*/
                    response.setAuthMsgId(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.CHANNEL_SEQ_NUM));
                } else {
                    response.setSignNo(acqResponse.getSgnNo());
                }

                response.setRespCode(acqResponse.getSysRtnCd());
                response.setRespDesc(acqResponse.getSysRtnDesc());
            }
        }

        bizResponse.setResult(response);


        return bizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        VerifySignResponse verifySignResponse = (VerifySignResponse) response.getResult();
        ConvertUtil.convertInput(verifySignResponse);
    }


    private VerifySignAcqRequest initVerifySignAcqRequest(XmlTreeUtil xmlTreeUtil, VerifySignRequest innerRequest) throws Exception {
        VerifySignAcqRequest verifySignAcqRequest = new VerifySignAcqRequest();

        SignInfoBean signInfo = new SignInfoBean();
        signInfo.setSgnAcctIssrId(innerRequest.getSgnAcctIssrId());
        signInfo.setSgnAcctTp(innerRequest.getSgnAcctTp());
        signInfo.setSgnAcctId(innerRequest.getSgnAcctId());
        signInfo.setSgnAcctNm(innerRequest.getSgnAcctNm());
        signInfo.setIdType(innerRequest.getIdType());
        signInfo.setIdNo(innerRequest.getIdNo());
        signInfo.setMobNo(innerRequest.getMobNo());
        verifySignAcqRequest.setSgnInf(signInfo);

        InstgInfBean instgInf = new InstgInfBean();
        instgInf.setInstgId(innerRequest.getInstgId());
        verifySignAcqRequest.setInstgInf(instgInf);
        verifySignAcqRequest.setTrxCtgy(innerRequest.getTrxCtgy());

        if (TrxCtgyCdEnum.SIGN.getType().equals(innerRequest.getTrxCtgy())) {
            /*快捷签约交易从原身份认证交易获取网联流水*/
            verifySignAcqRequest.setTrxId(innerRequest.getTrxId());
        } else {
            /*获取网联流水号*/
            NuccSeqNoManage NuccSeqNoManage = new NuccSeqNoManageImpl();
            String epccTxnId = NuccSeqNoManage.genEpccSeqNo();
            //todo 测试使用，提交代码前删除
            //epccTxnId = "2020040426162908080808080800001";
            verifySignAcqRequest.setTrxId(epccTxnId);
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.CHANNEL_SEQ_NUM, epccTxnId);
        }

        verifySignAcqRequest.setAuthMsg(innerRequest.getAuthMsg());
        verifySignAcqRequest.setClbckUrl(innerRequest.getClbckUrl());

        return verifySignAcqRequest;
    }


}
